﻿using fCloud.SensorApi.Models;
using fCloud.SensorApi.Services;
using Microsoft.AspNetCore.Mvc;

namespace fCloud.SensorApi.Controllers;
[ApiController]
[Route("api/[controller]/[action]")]
public class DeviceController( IServiceScopeFactory scopeFactory) : ControllerBase
{
    private readonly ITaskSchedulerService _taskSchedulerService=scopeFactory.CreateScope().ServiceProvider.GetRequiredService<ITaskSchedulerService>();
    [HttpGet]
    public async Task<IActionResult> GetDevices()
    {
        var devices =await _taskSchedulerService.GetSensorDetails();
       
        return Ok(devices);
    }

    [HttpGet]
    public async Task<IActionResult> GetPoints(string tag,string field,DateTime startTime,DateTime endTime)
    {
        var points =await _taskSchedulerService.GetPointsAsync("sensordatas",tag,field,startTime,endTime);
        return Ok(points);
    }

    [HttpPost]
    public async Task<IActionResult> GetSensorDataBySensorSn(string sensorSn,int currentPage,int pageSize)
    {
        var sensorData =await _taskSchedulerService.GetSensorDataBySensorSnWithPaging(sensorSn,currentPage,pageSize);
        return Ok(sensorData);
    }

    [HttpGet]
    public async Task<IActionResult> GetDeviceInfo()
    {
        var list = await _taskSchedulerService.GetDeviceList();
        var res = new CommonApiResponse()
        {
            Code = 200,
            Message = "success",
            Data = list
        };
        return Ok(res);
    }
}